package com.imooc.pan.server.common.mq.consumer.log;

import com.alibaba.fastjson.JSON;
import com.imooc.pan.core.utils.IdUtil;
//import com.imooc.pan.server.common.mq.config.RabbitMQConfig;
import com.imooc.pan.server.common.mq.event.log.ErrorLogEvent;
import com.imooc.pan.server.modules.log.entity.RPanErrorLog;
import com.imooc.pan.server.modules.log.service.IErrorLogService;
import lombok.extern.slf4j.Slf4j;
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

/**
 * 系统错误日志监听器
 */
@Component
@Slf4j
public class ErrorLogEventConsumer {

    @Autowired
    private IErrorLogService iErrorLogService;

    /**
     * 监听系统错误日志事件，并保存到数据库中
     *
     * @param message
     */
    //@StreamListener(PanChannels.ERROR_LOG_INPUT)
//    @RabbitListener(queues = RabbitMQConfig.ERROR_LOG_QUEUE)
    public void saveErrorLog(String message) {
        if (Objects.isNull(message)) {
            return;
        }
        log.info("error log message {} has been received", message);
        //printLog(message);
        ErrorLogEvent event = JSON.parseObject(message, ErrorLogEvent.class);
        //ErrorLogEvent event = message.getPayload();
        RPanErrorLog record = new RPanErrorLog();
        record.setId(IdUtil.get());
        record.setLogContent(event.getErrorMsg());
        record.setLogStatus(0);
        record.setCreateUser(event.getUserId());
        record.setCreateTime(new Date());
        record.setUpdateUser(event.getUserId());
        record.setUpdateTime(new Date());
        iErrorLogService.save(record);
    }
}
